﻿using System.IO;
using System.Xml;
using SoarD.Pandora;
using UnityEngine;

namespace SoarDEditor.Pandora
{
    public class PdrConfig
    {
        private static string PdrConfigPath { get { return Application.dataPath.Replace("/Assets", "/PdrConfig"); } }
        private static string PdrFileName { get { return "PdrConfig.xml"; } }
        private static string PdrConfigFullPath { get { return Path.Combine(PdrConfigPath, PdrFileName); } }

        private const string TITLE_CONFIG = "SoarDPandoraConfig";
        private const string TITLE_SETTING = "Settings";

        private static PdrConfig _instance;

        public static PdrConfig Instance
        {
            get
            {
                if (_instance != null) return _instance;
                _instance = new PdrConfig();
                _instance.Load();
                return _instance;
            }
        }

        public bool Load()
        {
            string configurationName = PdrConfigFullPath;
            if (!File.Exists(configurationName))
            {
                return false;
            }
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(configurationName);
            XmlNode xmlRoot = xmlDocument.SelectSingleNode(TITLE_CONFIG);
            XmlNode xmlSettings = xmlRoot.SelectSingleNode(TITLE_SETTING);
            XmlNodeList xmlNodeList = null;
            XmlNode xmlNode = null;

            xmlNodeList = xmlSettings.ChildNodes;
            for (int i = 0; i < xmlNodeList.Count; i++)
            {
                xmlNode = xmlNodeList.Item(i);
                switch (xmlNode.Name)
                {
                    case "UIPanelPath":
                        UIPanelPath = xmlNode.InnerText;
                        break;
                    case "UIAtlasPath":
                        UIAtlasPath = xmlNode.InnerText;
                        break;
                    case "UIAtlasTempPath":
                        UIAtlasTempPath = xmlNode.InnerText;
                        break;
                }
            }
            return true;
        }

        public bool Save()
        {
            string configurationName = PdrConfigFullPath;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null));

                XmlElement xmlRoot = xmlDocument.CreateElement(TITLE_CONFIG);
                xmlDocument.AppendChild(xmlRoot);

                XmlElement xmlSettings = xmlDocument.CreateElement(TITLE_SETTING);
                xmlRoot.AppendChild(xmlSettings);

                SaveElement(xmlDocument, xmlSettings, "UIAtlasPath", UIAtlasPath);
                SaveElement(xmlDocument, xmlSettings, "UIPanelPath", UIPanelPath);
                SaveElement(xmlDocument, xmlSettings, "UIAtlasTempPath", UIAtlasTempPath);

                PdrFileUtil.CreateDirectory(PdrConfigPath);
                xmlDocument.Save(configurationName);
                return true;
            }
            catch
            {
                File.Delete(configurationName);
                return false;
            }
        }

        private void SaveElement(XmlDocument xmlDocument, XmlElement xmlSettings, string name, string value)
        {
            XmlElement xmlElement = xmlDocument.CreateElement(name);
            xmlElement.InnerText = value;
            xmlSettings.AppendChild(xmlElement);
        }

        public string UIPanelPath
        {
            get;
            set;
        }

        public string UIAtlasPath
        {
            get;
            set;
        }

        public string UIAtlasTempPath
        {
            get;
            set;
        }

        public static string[] ToArray(string path)
        {
            return path.Split(';');
        }
    }
}
